package com.yaorange.jk.service.impl;

import com.yaorange.jk.dao.BaseDao;
import com.yaorange.jk.entity.Contract;
import com.yaorange.jk.entity.ContractProduct;
import com.yaorange.jk.service.ContractProductService;
import com.yaorange.jk.service.ContractService;
import com.yaorange.jk.utils.Pagination;
import com.yaorange.jk.utils.UtilFuns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author coach tam
 * @date 2017/11/1
 */
@Service("contractProductService")
@Transactional
public class ContractProductServiceImpl implements ContractProductService {


    @Autowired
    private BaseDao<ContractProduct,String> contractProductDao;


    @Autowired
    private ContractService contractService;
    @Override
    @Transactional(readOnly = true)
    public Pagination findPage(int pageNo, int pageSize, String id) {
        return contractProductDao.pageByHql("from ContractProduct cp where cp.contract.id = ?",pageNo,pageSize,id);
    }


    @Override
    public List<ContractProduct> findAll() {
        return contractProductDao.getListByHQL("from ContractProduct where state=1");
    }

    @Override
    public void save(ContractProduct contractProduct) {
        //计算总金额
        Double amount = contractProduct.getPrice()*contractProduct.getCnumber();
        contractProduct.setAmount(amount);


        //更新合同总金额
        Contract contract=contractService.findById(contractProduct.getContract().getId());
        //总金额=老总金额+货物总金额
        Double totalAmount= contract.getTotalAmount() + amount;
        contract.setTotalAmount(totalAmount);

        contractService.update(contract);


        contractProductDao.save(contractProduct);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.get(ContractProduct.class,id);
    }

    @Override
    public void update(ContractProduct model) {
        //更新货物总金额
        Double amount = model.getPrice()*model.getCnumber();
        model.setAmount(amount);

        //根据货物id货物 货物老数据
        ContractProduct oldCp = contractProductDao.get(ContractProduct.class,model.getId());


        //更新合同总金额
        Contract contract=contractService.findById(model.getContract().getId());
        //总金额=老总金额-货物老金额+货物新金额
        //货物老金额
        Double oldAmount= oldCp.getAmount();
        Double totalAmount= contract.getTotalAmount() - oldAmount + amount;
        contract.setTotalAmount(totalAmount);

        contractService.update(contract);

        contractProductDao.merge(model);
    }

    @Override
    public void deleteByIds(String[] ids) {
        for (String id :ids)
        {
            //根据货物id获取货物
            ContractProduct cp = contractProductDao.get(ContractProduct.class,id);

            //获取合同
            Contract contract = cp.getContract();
            //更新合同总金额 = 总金额 - 当前获取金额
            Double totalAmout = contract.getTotalAmount()-cp.getAmount();
            contract.setTotalAmount(totalAmout);

            contractService.update(contract);

            contractProductDao.deleteById(ContractProduct.class,id);
        }
    }

    @Override
    public List<ContractProduct> findListByShipTime(String inputDate) {
        return contractProductDao.getListByHQL("from ContractProduct cp where to_char(cp.contract.shipTime,'yyyy-MM') = ?",inputDate);
    }

    @Override
    public List<ContractProduct> findListByContractIds(String[] contractIds) {
        //contractIds数组转成  string  '','',''
        String ids = UtilFuns.joinInStr(contractIds);
        return contractProductDao.getListByHQL("from ContractProduct cp where cp.contract.id in ("+ids+")");
    }
}
